/**
 * Company : SIANGKLOM.
 * Created By : Chatree Siangklom
 * Created On : Oct 25, 2013  5:29:17 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.cases
 * File Name : UploadFileController.java
 * reference url :http://crunchify.com/spring-mvc-tutorial-how-to-upload-multiple-files-to-specific-location/
 * TODO
 */
package com.tis.cases;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.tis.interfaces.controller.commons.CommonController;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Oct 25, 2013 : 5:29:17 PM 
 * CreateBy=Chatree Siangklom
 * TODO
 */
@Controller
public class UploadFileController extends CommonController {
	
	private static final Logger logging = LoggerFactory.getLogger(UploadFileController.class);

	@RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String crunchifyDisplayForm() {
        return "/cases/uploadfile";
    }
 
    @RequestMapping(value = "/savefiles", method = RequestMethod.POST)
    public String crunchifySave(@ModelAttribute("uploadForm") UploadForm uploadForm, Model map) throws IllegalStateException, IOException {
        String saveDirectory = "c:/";
        
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Properties prop = new Properties();
        try {
        	
        	prop.load(classLoader.getResourceAsStream("/database/database-oracle.properties")); 
        	
        	String Username = prop.getProperty("username");
        	String Password = prop.getProperty("password");
        	
        } catch (IOException e) {
        	e.printStackTrace();
        }
 
        List<MultipartFile> files = uploadForm.getFiles();
 
        List<String> fileNames = new ArrayList<String>();
        if (null != files && files.size() > 0) {
            for (MultipartFile multipartFile : files) {
 
                String fileName = multipartFile.getOriginalFilename();
                if (!"".equalsIgnoreCase(fileName)) {
                    // Handle file content - multipartFile.getInputStream()
                    multipartFile.transferTo(new File(saveDirectory + fileName));
                    logging.info("fileName=" + fileName);
                    fileNames.add(fileName);
                }
            }
        }
 
        map.addAttribute("files", fileNames);
        return "/cases/uploadfilesuccess";
    }

}
